package com.example.dddsample.infrastructure.persistence;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.commom.model.PageRows;
import com.example.dddsample.domain.entity.CustomerAggregate;
import com.example.dddsample.domain.repository.CustomerRepository;
import com.example.dddsample.domain.vo.CustomerQueryBaseVO;
import com.example.dddsample.infrastructure.mapper.CustomerMapper;
import com.example.dddsample.infrastructure.persistence.assembler.CustomerPoAssembler;
import com.example.dddsample.infrastructure.persistence.po.CustomerPo;
import org.springframework.stereotype.Repository;

import java.util.Objects;

/**
 * @author: bebm
 * @date: 2022/3/29 13:44
 * @description:
 */
@Repository
public class CustomerRepositoryImpl extends ServiceImpl<CustomerMapper, CustomerPo>
        implements CustomerRepository {

    private final CustomerPoAssembler customerPoAssembler;


    public CustomerRepositoryImpl(CustomerPoAssembler customerPoAssembler) {
        this.customerPoAssembler = customerPoAssembler;
    }

    /**
     * @param custId
     * @return
     */
    public CustomerPo selectById(String custId) {
        return this.baseMapper.selectById(custId);
    }

    /**
     * @param custId
     * @return
     */
    @Override
    public CustomerAggregate getCustomerAggregate(String custId) {
        CustomerPo customerPo = this.selectById(custId);
        return customerPoAssembler.assemblerFromCustomerPo(customerPo);
    }

    /**
     * getPageCustomerAggregate
     * @param customerQueryBaseVO
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageRows getPageCustomerAggregate(CustomerQueryBaseVO customerQueryBaseVO, int pageNum, int pageSize) {
        Page page=new Page(pageNum,pageSize);
        /*LambdaQueryWrapper<CustomerPo> customerQueryWrapper=new LambdaQueryWrapper<>();
        if(!Objects.isNull(customerQueryBaseVO.getCustName())) {
            customerQueryWrapper.like(CustomerPo::getCustName, customerQueryBaseVO.getCustName());
        }
        IPage<CustomerPo> customerPoIpage=this.baseMapper.selectPage(page,customerQueryWrapper);*/
        IPage<CustomerPo> customerPoIpage=this.baseMapper.getAll(page,customerQueryBaseVO);
        return customerPoAssembler.assemblerFromCustomerPo(customerPoIpage);
    }
}
